PyQt4 QCompleter ordering

by: MWoodin, 6 years ago

Last edited: 6 years ago

So I'm trying to create a search widget for an external app. I am 90% the way there but there is one slight hurdle and that's the order that the list is displayed.

<pre class='prettyprint lang-py'>from PyQt4 import QtGui, QtCore


class TabWidget(QtGui.QMainWindow):

    def __init__(self):
        super(TabWidget, self).__init__()

        self.input = QtGui.QLineEdit()

        self.custom_auto_completer = CustomCompleter()
        self.custom_auto_completer.setCompletionMode(QtGui.QCompleter.PopupCompletion)
        self.custom_auto_completer.setCaseSensitivity(0)
        self.input.setCompleter(self.custom_auto_completer)

        new_list = ['apple', 'carrot', 'commendable', 'appendable', 'happen', 'dab']

        model = QtGui.QStringListModel(new_list)
        self.custom_auto_completer.setModel(model)

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.Popup)
        self.setCentralWidget(self.input)
        self.resize(200, 20)
        self.setStyleSheet("margin:5px; border:5px solid rgb(10, 10, 10); border-radius:10px")
        self.input.setFocus()

        self.comp = None

        self.show()

    def showEvent(self, event):
        position = self.frameGeometry()
        position.moveCenter(QtGui.QCursor.pos())
        self.setGeometry(position)
        super(TabWidget, self).showEvent(event)


class CustomCompleter(QtGui.QCompleter):

    def __init__(self):
        super(CustomCompleter, self).__init__()
        self.local_completion_prefix = ""
        self.source_model = None
        self.filterProxyModel = QtGui.QSortFilterProxyModel(self)
        self.usingOriginalModel = False

    def setModel(self, model):
        self.source_model = model
        self.filterProxyModel = QtGui.QSortFilterProxyModel(self)
        self.filterProxyModel.setSourceModel(self.source_model)
        super(CustomCompleter, self).setModel(self.filterProxyModel)
        self.usingOriginalModel = True

    def updateModel(self):
        if not self.usingOriginalModel:
            self.filterProxyModel.setSourceModel(self.source_model)

        pattern = QtCore.QRegExp(self.local_completion_prefix, QtCore.Qt.CaseInsensitive, QtCore.QRegExp.FixedString)

        self.filterProxyModel.setFilterRegExp(pattern)

    def splitPath(self, path):
        self.local_completion_prefix = path
        self.updateModel()

        if self.filterProxyModel.rowCount() == 0:
            self.usingOriginalModel = False
            self.filterProxyModel.setSourceModel(QtGui.QStringListModel([path]))
            return [path]
        else:
            return QtCore.QStringList()


tab_search = 0


def main():
    global tab_search
    tab_search = TabWidget()
    tab_search.show()
    return tab_search

if __name__ == '__main__':
    app = QtGui.QApplication([])
    main()
    app.exec_()
</pre>

So at the moment if I search for the word 'dab' the words commendable and appendable will show up before dab but I would like dab to show up first. I'm very new to pyqt and I have found a couple of examples that apparently work in qt but they are written in C++ and I'm not very good at translating that into python code.

Any help would be super appreciated.

Thanks!



You must be logged in to post. Please login or register an account.